package com.zyc;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author zyc
 * @Date 2019/4/17 10:55
 * @Description 自定义plug
 */
public class MyBatisPlusGenerator {
    /**
     * 生成MybatisGenerator工具
     *
     * @throws Exception
     */
    public static void mybatisGenerator() throws Exception {
        List<String> warnings = new ArrayList<String>();
        boolean overwrite = true;
        //指定 逆向工程配置文件
        URL resource = Thread.currentThread().getContextClassLoader().getResource("generatorConfig.xml");
        if(null != resource){
            ConfigurationParser cp = new ConfigurationParser(warnings);
            Configuration config = cp.parseConfiguration(new File(resource.getFile().substring(1)));
            DefaultShellCallback callback = new DefaultShellCallback(overwrite);
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
                    callback, warnings);
            myBatisGenerator.generate(null);
        }
    }

    public static void main(String[] args) throws Exception {
        try {
            MyBatisPlusGenerator.mybatisGenerator();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.exit(0);
    }

}
